package com.leyou.web;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.vo.ExceptionResult;
import com.leyou.entity.Item;
import com.leyou.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2019/5/2 21:39
 * @description TODO
 **/
@RestController
@RequestMapping("item")
public class ItemController{
    @Autowired
    ItemService service;

    @PostMapping
    public ResponseEntity<Item> saveItem(Item item) throws RuntimeException{
        //校验数据
        if(item.getPrice() == null){
            //数据有误，返回400
           // return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            //抛出运行时异常，springmvc会自动处理，而且事务还会回滚
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }

        Item result = service.saveItem(item);
        //新增成功返回201
        return ResponseEntity.status(HttpStatus.CREATED).body(result);
    }
}
